package kr.ac.skhu.m.data;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import net.htmlparser.jericho.Element;
import net.htmlparser.jericho.HTMLElementName;
import net.htmlparser.jericho.Source;

import kr.ac.skhu.m.value.NoticeListValue;
import kr.ac.skhu.m.value.SkhuNewsValue;

public class SkhuNewsData {
	
	private int intTypeCurPage;
	private int intTypeTotalPage;
	private String url;
	
	public int getIntTypeCurPage() {
		return intTypeCurPage;
	}

	public void setIntTypeCurPage(int intTypeCurPage) {
		this.intTypeCurPage = intTypeCurPage;
	}

	public int getIntTypeTotalPage() {
		return intTypeTotalPage;
	}

	public void setIntTypeTotalPage(int intTypeTotalPage) {
		this.intTypeTotalPage = intTypeTotalPage;
	}
	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}
	
	public SkhuNewsData(String url) {
		this.url = url;
	}
	
	public SkhuNewsData() {
	}

	public SkhuNewsValue getNoticeListValue() throws Exception {
		
		//해당 URL 페이지를 가져온다.
		Source source = new Source(new URL(url));

		//메소드 찾기를 위해 시작부터 끝까지 태그들만 parse 한다 (?)
		source.fullSequentialParse();
		
		/**
		 * 목록 생성
		 */
		Element board_list2 = source.getAllElementsByClass("board_list2").get(0);
		Element list = board_list2.getAllElements(HTMLElementName.LI).get(0);
		SkhuNewsValue skhuNews = new SkhuNewsValue();
		
		String href = list.getAllElements(HTMLElementName.H5).get(0).getAllElements(HTMLElementName.A).get(0).getAttributeValue("href");
		href = href.replace("boardread.aspx?", "");
		String img = list.getAllElements(HTMLElementName.IMG).get(0).getAttributeValue("src");
		String title = list.getAllElements(HTMLElementName.H5).get(0).getAllElements(HTMLElementName.A).get(0).getTextExtractor().toString();
		String content = list.getAllElementsByClass("desc").get(0).getTextExtractor().toString();
		
		skhuNews.setContent(content);
		skhuNews.setIdx(href);
		skhuNews.setImg(img);
		skhuNews.setTitle(title);
		
		return skhuNews;
	}
	
	public List<SkhuNewsValue> getSkhuListValue() throws Exception {
		
		//해당 URL 페이지를 가져온다.
		Source source = new Source(new URL(url));

		//메소드 찾기를 위해 시작부터 끝까지 태그들만 parse 한다 (?)
		source.fullSequentialParse();
		
		/**
		 * 목록 생성
		 */
		Element board_list2 = source.getAllElementsByClass("board_list2").get(0);
		List<Element> list = board_list2.getAllElements(HTMLElementName.LI);
		int listSize = list.size();
		
		List<SkhuNewsValue> skhuNewsList = new ArrayList<SkhuNewsValue>();
		
		for(int i=0; i<listSize; i++){
			SkhuNewsValue skhuNews = new SkhuNewsValue();
			
			String href = list.get(i).getAllElements(HTMLElementName.H5).get(0).getAllElements(HTMLElementName.A).get(0).getAttributeValue("href");
			href = href.replace("boardread.aspx?", "");
			String img = list.get(i).getAllElements(HTMLElementName.IMG).get(0).getAttributeValue("src");
			String title = list.get(i).getAllElements(HTMLElementName.H5).get(0).getAllElements(HTMLElementName.A).get(0).getTextExtractor().toString();
			String content = list.get(i).getAllElementsByClass("desc").get(0).getTextExtractor().toString();
			
			if(img.contains("btn")){
				img = "";
			}
			
			skhuNews.setContent(content);
			skhuNews.setIdx(href);
			skhuNews.setImg(img);
			skhuNews.setTitle(title);
			
			skhuNewsList.add(skhuNews);
		}
		
		/**
		 * 페이징 정보
		 */
		//현재 페이지 번호
		Element cur = source.getAllElementsByClass("cur").get(0);
		String curPage = cur.getTextExtractor().toString();
		this.intTypeCurPage = Integer.parseInt(curPage);
		
		//전체 페이지 수
		Element nextL = source.getAllElementsByClass("nextL").get(0);
		String nextLString = nextL.getAttributeValue("href");
		int nextLBegin = nextLString.indexOf("curpage=");
		int nextLEnd = nextLString.indexOf("&");
		String totalPage = nextLString.substring(nextLBegin + 8, nextLEnd);
		this.intTypeTotalPage = Integer.parseInt(totalPage);
		
		return skhuNewsList;
	}
	
}
